package com.noahsloan.groovy.util



/**
 * For comparable purposes, Infinity - Infinity = 0
 * 
 * @author noah
 *
 */
public final class Infinity extends Number implements Comparable<Number> {
	
	static final INFINITY = new Infinity()
	
	int compareTo(Object o) {
		if(o instanceof Infinity) return 0
		if(o instanceof Number) return 1
		throw new ClassCastException(
				"Cannot compare non-Number $o to Infinity")
	}
	
	double doubleValue() { Double.POSITIVE_INFINITY }
	
	long longValue() { Long.MAX_VALUE }
	
	int intValue() { Integer.MAX_VALUE }
	
	float floatValue() { Float.POSITIVE_INFINITY }

	Number minus(Number n) { INFINITY }
	Number minus(Infinity n) { 0 } // technically incorrect, but necessary
	
	static def nullCheck(num) { num == null ? INFINITY : num }
	
	static int compare(Number a, Number b) {
		nullCheck(a) - nullCheck(b)
	}
	
}
